package cz.muni.fi.rum.gui;

/*
 * MainFrame.java
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.jdesktop.application.FrameView;
import javax.swing.Icon;
import javax.swing.Timer;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.TaskMonitor;
import org.noos.xing.mydoggy.ToolWindow;
import org.noos.xing.mydoggy.ToolWindowType;

/**
 * The application's main frame.
 */
public class MainFrame extends FrameView {

//    public static final String MODEL_ID = "Model";
//    public static final String HISTORY_ID = "History";
//    public static final String MONITOR_ID = "Monitor";
//    public static final String CONNECTOR_ID = "Connector";
//    public static final String SERVER_ID = "Servers";

    public MainFrame(SingleFrameApplication app) {
        super(app);
        //TODO == Main frame== ziskani resource a actionMapy
//ActionMap reflectorActionMap = RumConfigurator.getApplication().getContext().getActionMap(reflectorPane);
//ResourceMap reflectorResourceMap = RumConfigurator.getApplication().getContext().getResourceMap(ReflektorTabComponent.class);
        initComponents();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        initStatusBar();
        setComponent(MyDoggyController.getInstance().getWindowManager());

    }
// <editor-fold defaultstate="collapsed" desc="Init Status bar">

    private void initStatusBar() {
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                        statusMessageLabel.setText("");
//                        statusMessageLabel.setText(evt.toString());
//                    System.out.println("Started ");
//                        System.out.println("EVT: ");
//                    System.out.println(evt);
//                    System.out.println();
//                    System.out.println("propertyName: ");
//                    System.out.println(evt.getPropertyName());
//                    System.out.println();
//                    System.out.println("EVT nEW value: ");
//                    System.out.println(evt.getNewValue());
//                    System.out.println();
//                    System.out.println("EVT old value: ");
//                    System.out.println(evt.getOldValue());
//                    System.out.println();
//                    System.out.println("EVT propagationId");
//                    System.out.println(evt.getPropagationId());
                    }
                    //progressBar.setVisible(true);
                    //progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    statusMessageLabel.setText("");
//                    System.out.println("Done");
//                    System.out.println("EVT: ");
//                    System.out.println(evt);
//                    System.out.println();
//                    System.out.println("propertyName: ");
//                    System.out.println(evt.getPropertyName());
//                    System.out.println();
//                    System.out.println("EVT nEW value: ");
//                    System.out.println(evt.getNewValue());
//                    System.out.println();
//                    System.out.println("EVT old value: ");
//                    System.out.println(evt.getOldValue());
//                    System.out.println();
//                    System.out.println("EVT propagationId");
//                    System.out.println(evt.getPropagationId());
//                    statusMessageLabel.setText(evt.toString());
//                    progressBar.setVisible(true);
                    //progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }
// </editor-fold>

    public void showFloatingConnector() {
        ToolWindow connector = MyDoggyController.getInstance().getWindowManager().getToolWindow(MyDoggyController.CONNECTOR_ID);
        connector.setType(ToolWindowType.FLOATING_LIVE);
        connector.setActive(true);
    }

    @Action
    public void showConnectorToolWindowAction() {
        MyDoggyController.getInstance().showToolWindow(MyDoggyController.CONNECTOR_ID);
    }

    @Action
    public void showServerToolWindowAction() {
        MyDoggyController.getInstance().showToolWindow(MyDoggyController.SERVER_ID);
    }
    @Action
    public void showHistoryToolWindowAction() {
        MyDoggyController.getInstance().showToolWindow(MyDoggyController.HISTORY_ID);
    }
    @Action
    public void showModelToolWindowAction() {
        MyDoggyController.getInstance().showToolWindow(MyDoggyController.MODEL_ID);
    }
    @Action
    public void showMonitorToolWindowAction() {
        MyDoggyController.getInstance().showToolWindow(MyDoggyController.MONITOR_ID);
    }
    @Action
    public void showDefaultDesktopAction() {
        MyDoggyController.getInstance().showBaseDesktop();
    }

//    @Deprecated
//    @Action
//    public Task connectAction() {
//
//        if (connectionBox.isMakeConnect()) {
////            reflectorPane.getReflector().reset();
////            Task retTask = reflectorPane.connectAction(connectionBox.getSelectedServer());
//            //Zobrazeni panelu do jineho panelu
//            org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
//            mainPanel.setLayout(mainPanelLayout);
//
//            mainPanelLayout.setHorizontalGroup(
//                    mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(reflectorPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
//            mainPanelLayout.setVerticalGroup(
//                    mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(reflectorPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
////            connectServerItem.setEnabled(false);
////            disconnectServerItem.setEnabled(true);
////            disconnectAndCloseItem.setEnabled(true);
////            synchronizeItemMenu.doClick();
////            sendRequestsItemMenu.doClick();
////            reflectorPane.setVisible(true);
//            return retTask;
//        }
//        return null;
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        reflectorActionMenu = new javax.swing.JMenu();
        toolMenu = new javax.swing.JMenu();
        resetToDefaultWiewItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        modelToolItem = new javax.swing.JMenuItem();
        historyToolItem = new javax.swing.JMenuItem();
        monitorToolItem = new javax.swing.JMenuItem();
        connectionToolItem = new javax.swing.JMenuItem();
        serverToolItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(824, 636));

        org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 1065, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 637, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getResourceMap(MainFrame.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getActionMap(MainFrame.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        reflectorActionMenu.setText(resourceMap.getString("reflectorActionMenu.text")); // NOI18N
        reflectorActionMenu.setName("reflectorActionMenu"); // NOI18N
        menuBar.add(reflectorActionMenu);

        toolMenu.setText(resourceMap.getString("toolMenu.text")); // NOI18N
        toolMenu.setName("toolMenu"); // NOI18N

        resetToDefaultWiewItem.setAction(actionMap.get("showDefaultDesktopAction")); // NOI18N
        resetToDefaultWiewItem.setText(resourceMap.getString("resetToDefaultWiewItem.text")); // NOI18N
        resetToDefaultWiewItem.setName("resetToDefaultWiewItem"); // NOI18N
        toolMenu.add(resetToDefaultWiewItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        toolMenu.add(jSeparator1);

        modelToolItem.setAction(actionMap.get("showModelToolWindowAction")); // NOI18N
        modelToolItem.setText(resourceMap.getString("modelToolItem.text")); // NOI18N
        modelToolItem.setName("modelToolItem"); // NOI18N
        toolMenu.add(modelToolItem);

        historyToolItem.setAction(actionMap.get("showHistoryToolWindowAction")); // NOI18N
        historyToolItem.setText(resourceMap.getString("historyToolItem.text")); // NOI18N
        historyToolItem.setName("historyToolItem"); // NOI18N
        toolMenu.add(historyToolItem);

        monitorToolItem.setAction(actionMap.get("showMonitorToolWindowAction")); // NOI18N
        monitorToolItem.setText(resourceMap.getString("monitorToolItem.text")); // NOI18N
        monitorToolItem.setName("monitorToolItem"); // NOI18N
        toolMenu.add(monitorToolItem);

        connectionToolItem.setAction(actionMap.get("showConnectorToolWindowAction")); // NOI18N
        connectionToolItem.setText(resourceMap.getString("connectionToolItem.text")); // NOI18N
        connectionToolItem.setName("connectionToolItem"); // NOI18N
        toolMenu.add(connectionToolItem);

        serverToolItem.setAction(actionMap.get("showServerToolWindowAction")); // NOI18N
        serverToolItem.setText(resourceMap.getString("serverToolItem.text")); // NOI18N
        serverToolItem.setName("serverToolItem"); // NOI18N
        toolMenu.add(serverToolItem);

        menuBar.add(toolMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("showAboutBox.Action.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1065, Short.MAX_VALUE)
            .add(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(statusMessageLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 895, Short.MAX_VALUE)
                .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(statusPanelLayout.createSequentialGroup()
                .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(statusMessageLabel)
                    .add(statusAnimationLabel)
                    .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem;
    private javax.swing.JMenuItem connectionToolItem;
    private javax.swing.JMenuItem historyToolItem;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem modelToolItem;
    private javax.swing.JMenuItem monitorToolItem;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JMenu reflectorActionMenu;
    private javax.swing.JMenuItem resetToDefaultWiewItem;
    private javax.swing.JMenuItem serverToolItem;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JMenu toolMenu;
    // End of variables declaration//GEN-END:variables
    private Timer messageTimer;
    private Timer busyIconTimer;
    private Icon idleIcon;
    private Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
}
